#include "user_include.h"
#include "stdio.h"
#include "cJSON.h"

void user_status_report_ble(uint8_t wifi_status, uint8_t mqtt_status)
{
    cJSON *root = NULL, *data = NULL;
    root = cJSON_CreateObject();
    if (root == NULL) {
	    return ;
    }
    cJSON_AddStringToObject(root, "cmd", "netStatus");
    data = cJSON_CreateObject();
    if (root == NULL) {
	    cJSON_Delete(root);
    	return ;
    }
    cJSON_AddNumberToObject(data, "wifi", wifi_status);
    cJSON_AddNumberToObject(data, "mqtt", mqtt_status);
    cJSON_AddItemToObject(root, "data", data);

    // data = cJSON_CreateObject();

    char *str = cJSON_PrintUnformatted(root);
    // printf("%s jsonStr:[%s]\r\n", __func__, str);

    char *ble_data = NULL;
    ble_data = malloc(strlen(str) + 4);

    if (ble_data == NULL) {
        cJSON_free(str);
        cJSON_Delete(root);
        printf("%s malloc error\r\n", __func__);
        return ;
    }
    ble_data[0] = 0xaa;
    ble_data[1] = 0xa3;
    ble_data[2] = strlen(str);
    memcpy(&ble_data[3], str, strlen(str));
    user_ble_send_data((uint8_t *)ble_data, strlen(str) + 3);
    free(ble_data);
    cJSON_free(str);
    cJSON_Delete(root);
}

void led_status_report_ble(uint8_t status)
{
    cJSON *root = NULL, *data = NULL;
    root = cJSON_CreateObject();
    if (root == NULL) {
	    return ;
    }
    cJSON_AddStringToObject(root, "cmd", "ledStatusReport");
    data = cJSON_CreateObject();
    if (root == NULL) {
	    cJSON_Delete(root);
    	return ;
    }
    cJSON_AddNumberToObject(data, "led", status);
    cJSON_AddItemToObject(root, "data", data);

    char *str = cJSON_PrintUnformatted(root);
    // printf("%s jsonStr:[%s]\r\n", __func__, str);
    char *ble_data = NULL;
    ble_data = malloc(strlen(str) + 4);
    if (ble_data == NULL) {
        cJSON_free(str);
        cJSON_Delete(root);
        printf("%s malloc error\r\n", __func__);
        return ;
    }
    ble_data[0] = 0xaa;
    ble_data[1] = 0xa4;
    ble_data[2] = strlen(str);
    memcpy(&ble_data[3], str, strlen(str));
    user_ble_send_data((uint8_t *)ble_data, strlen(str) + 3);
    free(ble_data);
    cJSON_free(str);
    cJSON_Delete(root);
}

void led_array_status_report_mqtt(uint8_t index, uint8_t status)
{
    cJSON *root = NULL, *array= NULL, *data = NULL;
    root = cJSON_CreateObject();
    if (root == NULL) {
	    return ;
    }
    cJSON_AddStringToObject(root, "cmd", "reportLedStatus");
    data = cJSON_CreateObject();
    if (root == NULL) {
	    cJSON_Delete(root);
	    return ;
    }
    cJSON_AddNumberToObject(data, "index", index);
    cJSON_AddNumberToObject(data, "status", status);
    array = cJSON_CreateArray();

    if (array == NULL) {
	    cJSON_Delete(root);
	    return ;
    }
    cJSON_AddItemToArray(array, data);
    cJSON_AddItemToObject(root, "data", array);
    char *str = cJSON_PrintUnformatted(root);
    // printf("%s jsonStr:[%s]\r\n", __func__, str);
    memset(mqtt_send_flag.data[mqtt_send_flag.tail].data, 0, sizeof(mqtt_send_flag.data[mqtt_send_flag.tail].data));
    memcpy(mqtt_send_flag.data[mqtt_send_flag.tail].data, str, strlen(str));
    mqtt_send_flag.tail = (mqtt_send_flag.tail + 1) % MQTT_SEND_MAX;
    cJSON_free(str);
    cJSON_Delete(root);
}


void led_status_report_mqtt(uint8_t status)
{
    cJSON *root = NULL, *data = NULL;
    root = cJSON_CreateObject();
    if (root == NULL) {
	    return ;
    }
    cJSON_AddStringToObject(root, "cmd", "reportLedStatus");
    data = cJSON_CreateObject();
    if (root == NULL) {
	    cJSON_Delete(root);
	    return ;
    }
    cJSON_AddNumberToObject(data, "led", status);
    cJSON_AddItemToObject(root, "data", data);
    char *str = cJSON_PrintUnformatted(root);
    // printf("%s jsonStr:[%s]\r\n", __func__, str);
    memset(mqtt_send_flag.data[mqtt_send_flag.tail].data, 0, sizeof(mqtt_send_flag.data[mqtt_send_flag.tail].data));
    memcpy(mqtt_send_flag.data[mqtt_send_flag.tail].data, str, strlen(str));
    mqtt_send_flag.tail = (mqtt_send_flag.tail + 1) % MQTT_SEND_MAX;
    cJSON_free(str);
    cJSON_Delete(root);
}

void aht20_data_report_mqtt(aht20_flag_t aht20)
{
    cJSON *root = NULL, *data = NULL;
    root = cJSON_CreateObject();
    if (root == NULL) {
	    return ;
    }
    cJSON_AddStringToObject(root, "cmd", "reportAht20Data");
    data = cJSON_CreateObject();
    if (root == NULL) {
	    cJSON_Delete(root);
	    return ;
    }
    cJSON_AddNumberToObject(data, "temperature", aht20.temp);
    cJSON_AddNumberToObject(data, "huminity", aht20.humi);
    cJSON_AddItemToObject(root, "data", data);
    char *str = cJSON_PrintUnformatted(root);
    // printf("%s jsonStr:[%s]\r\n", __func__, str);
    memset(mqtt_send_flag.data[mqtt_send_flag.tail].data, 0, sizeof(mqtt_send_flag.data[mqtt_send_flag.tail].data));
    memcpy(mqtt_send_flag.data[mqtt_send_flag.tail].data, str, strlen(str));
    mqtt_send_flag.tail = (mqtt_send_flag.tail + 1) % MQTT_SEND_MAX;
    cJSON_free(str);
    cJSON_Delete(root);
}


void aht20_data_report_ble(aht20_flag_t aht20)
{
    cJSON *root = NULL, *data = NULL;
    root = cJSON_CreateObject();
    if (root == NULL) {
	    return ;
    }
    cJSON_AddStringToObject(root, "cmd", "reportAht20Data");
    data = cJSON_CreateObject();
    if (root == NULL) {
	    cJSON_Delete(root);
	    return ;
    }
    cJSON_AddNumberToObject(data, "temperature", aht20.temp);
    cJSON_AddNumberToObject(data, "huminity", aht20.humi);
    cJSON_AddItemToObject(root, "data", data);
    char *str = cJSON_PrintUnformatted(root);
    // printf("%s jsonStr:[%s]\r\n", __func__, str);

    char *ble_data = NULL;
    ble_data = malloc(strlen(str) + 4);
    if (ble_data == NULL) {
        cJSON_free(str);
        cJSON_Delete(root);
        printf("%s malloc error\r\n", __func__);
        return ;
    }
    ble_data[0] = 0xaa;
    ble_data[1] = 0xa5;
    ble_data[2] = strlen(str);
    memcpy(&ble_data[3], str, strlen(str));
    user_ble_send_data((uint8_t *)ble_data, strlen(str) + 3);
    free(ble_data);
    cJSON_free(str);
    cJSON_Delete(root);
}